﻿using DataLibrary;
using System;
using System.Linq;
using System.Windows.Forms;
using Common;
using LinqKit;
using System.Collections;
using System.Collections.Generic;

namespace TicketSystem
{
    public partial class Form_Back : Form
    {
        private TicketSystemEntities tse = new TicketSystemEntities();
        private Guid _saleGuid;
        public Form_Back()
        {
            InitializeComponent();
        }

        private void InitData()
        {
            txt_BackOne.Text = "";
            txt_BackPark.Text = "";
            txt_BackParkSpecial.Text = "";
            txt_BackTwo.Text = "";
            txt_BackThree.Text = "";
            dataGridView1.DataSource = null;           
        }

        private void BindData()
        {
            //txt_BackOne.Text = ""; txt_BackPark.Text = ""; txt_BackParkSpecial.Text = ""; txt_BackTwo.Text = ""; txt_BackThree.Text = "";
            //dataGridView1.DataSource = null;
            //InitData();
            var result = (IQueryable<Sales>)tse.Sales;
            var _where = PredicateBuilder.New<Sales>(true);
            if (!string.IsNullOrEmpty(txt_SaleInfo.Text))
            {
                if (txt_SaleInfo.Text.Contains("-"))
                {
                    try
                    {
                        Guid guid = Guid.Parse(txt_SaleInfo.Text);
                        _where = _where.And(u => u.SaleGuid == guid);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("格式不合法:" + ex.Message);
                        return;
                    }
                }
                else
                {
                    try
                    {
                        int saleid = int.Parse(txt_SaleInfo.Text);
                        _where = _where.And(u => u.SaleID == saleid);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("格式不合法:" + ex.Message);
                        return;
                    }
                }
            }
            if (!string.IsNullOrEmpty(cBox_Operator.Text))
            {
                int operatorid = (int)cBox_Operator.SelectedValue;
                _where = _where.And(u => u.OperatorID == operatorid);
            }
            if(!string.IsNullOrEmpty(txt_department.Text))
            {
                _where = _where.And(u => u.DepartmentsGuides.Departments.DepartmentName.Contains(txt_department.Text));
            }

            if (!string.IsNullOrEmpty(txt_Guide.Text))
            {
                _where = _where.And(u => u.DepartmentsGuides.Guides.Name.Contains(txt_Guide.Text));
            }

            if(checkBox_DateSpan.Checked)
            {
                DateTime start = DateTime.Parse(Convert.ToDateTime(dTP_Date.Text).ToString("yyyy-MM-dd ") + " 00:00:00");
                DateTime end = DateTime.Parse(Convert.ToDateTime(dTP_Date.Text).ToString("yyyy-MM-dd ") + " 23:59:59");
                _where = _where.And(u => u.Created_at > start && u.Created_at <= end);
            }
            //_where = _where.And(u => u.BackFlag == 0 );//不查询退票和全退的订单：backflag：0，1-出票（1用来标识全退的票），2-退票
            result = tse.Sales.AsExpandable().Where(_where);
            if (result.Count() > 0)
            {
                List<Sales> list = result.ToList();
                GetDataGridView(list);
                txt_One.Text = result.FirstOrDefault().TicketOne.ToString();
                txt_Two.Text = result.FirstOrDefault().TicketTwo.ToString();
                txt_Three.Text = result.FirstOrDefault().TicketThree.ToString();
                txt_Park.Text = result.FirstOrDefault().TicketPark.ToString();
                txt_ParkSpecial.Text = result.FirstOrDefault().TicketParkSpecial.ToString();
                lb_SaleGuid.Text = result.FirstOrDefault().SaleGuid.ToString();
                _saleGuid = result.FirstOrDefault().SaleGuid;
                int[] canBacks = GetBackCount(_saleGuid);
                txt_CanBackOne.Text = canBacks[0].ToString();
                txt_CanBackPark.Text = canBacks[1].ToString();
                txt_CanBackParkSpecial.Text = canBacks[2].ToString();
                txt_CanBackTwo.Text = canBacks[3].ToString();
                txt_CanBackThree.Text = canBacks[4].ToString();
                if (result.Count() == 1)
                {
                    int saleid = result.First().SaleID;
                    if (result.FirstOrDefault().Created_at.Date == DateTime.Now.Date && tse.Backs.Where(u => u.SaleID == saleid).Count() == 0)
                    {
                        DialogResult d = MessageBox.Show("当天的票只能全退，是否继续", "确认退票", MessageBoxButtons.YesNo);
                        if (d == DialogResult.Yes)
                        {
                            var oldSale = result.FirstOrDefault();
                            txt_BackOne.Text = oldSale.TicketOne.ToString(); txt_BackPark.Text = oldSale.TicketPark.ToString();
                            txt_BackParkSpecial.Text = oldSale.TicketParkSpecial.ToString(); txt_BackTwo.Text = oldSale.TicketTwo.ToString();
                            txt_BackThree.Text = oldSale.TicketThree.ToString();
                            oldSale.BackFlag = 1;
                            Backs back = new Backs();
                            back.BackOne = oldSale.TicketOne;
                            back.BackTwo = oldSale.TicketTwo;
                            back.BackThree = oldSale.TicketThree;
                            back.BackPark = oldSale.TicketPark;
                            back.BackParkSpecial = oldSale.TicketParkSpecial;
                            //back.BackMoney = oldSale.PurchaseMoney + oldSale.SignedMoney;//退款总金额
                            back.BackMoney = oldSale.PurchaseMoney;//退款总金额
                            back.BackNumber = oldSale.SaleGuid.ToString();
                            DateTime todaystart = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd ") + " 00:00:00");
                            int cnt = tse.Checkouts.Where(u => u.Checkout_at > todaystart).Count();
                            if (cnt > 0)
                            {
                                back.Created_at = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("yyyy-MM-dd ") + " 00:00:01");
                                back.Updated_at = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("yyyy-MM-dd ") + " 00:00:01");
                            }
                            else
                            {
                                back.Created_at = DateTime.Now;
                                back.Updated_at = DateTime.Now;
                            }
                            back.SaleID = oldSale.SaleID;
                            back.OperatorID = GetOperator.OperatorID;
                            tse.Backs.Add(back);
                            tse.SaveChanges();
                            MessageBox.Show("票号为:" + oldSale.SaleGuid + "(" + oldSale.SaleID + ")" + "的订票，已退票成功!");
                            BindData();
                            txt_SaleInfo.Focus();
                            SendKeys.Send("{END}+{HOME}");
                        }
                        txt_BackOne.Enabled = false; txt_BackPark.Enabled = false;
                        txt_BackParkSpecial.Enabled = false; txt_BackTwo.Enabled = false;
                        txt_BackThree.Enabled = false; btn_Update.Enabled = false;
                    }
                    else
                    {
                        txt_BackOne.Enabled = true; txt_BackPark.Enabled = true;
                        txt_BackParkSpecial.Enabled = true; txt_BackTwo.Enabled = true;
                        txt_BackThree.Enabled = true; btn_Update.Enabled = true;
                    }
                }
            }
            else
                MessageBox.Show("找不到任何数据");
            dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
        }


        private DataGridView GetDataGridView(List<Sales> sales)
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("首套船");
            dt.Columns.Add("入园普通票");
            dt.Columns.Add("入园学生票");
            dt.Columns.Add("颐内一道船");
            dt.Columns.Add("颐内二道船");
            dt.Columns.Add("导游姓名");
            dt.Columns.Add("旅行社");
            dt.Columns.Add("签单人数");
            dt.Columns.Add("签单号");
            dt.Columns.Add("签单金额");
            dt.Columns.Add("非签单金额");
            dt.Columns.Add("开票时间");
            dt.Columns.Add("流水号");
            dt.Columns.Add("票号");
            dt.Columns.Add("操作员");
            //dt.Columns.Add("类型");
            //dt.Columns.Add("原始票号");
            for (int i = 0; i < sales.Count; i++)
            {
                List<string> list = new List<string>(dt.Columns.Count);
                list.Add(sales[i].TicketOne.ToString());
                list.Add(sales[i].TicketPark.ToString());
                list.Add(sales[i].TicketParkSpecial.ToString());
                list.Add(sales[i].TicketTwo.ToString());
                list.Add(sales[i].TicketThree.ToString());
                list.Add(sales[i].DepartmentsGuides.Guides.Name.ToString());
                list.Add(sales[i].DepartmentsGuides.Departments.DepartmentName.ToString());
                list.Add(sales[i].SignedPeople.ToString());
                list.Add(sales[i].SignedNumber.ToString());
                list.Add(sales[i].SignedMoney.ToString());
                list.Add(sales[i].PurchaseMoney.ToString());
                list.Add(sales[i].Created_at.ToString());
                list.Add(sales[i].SaleID.ToString());
                list.Add(sales[i].SaleGuid.ToString());
                list.Add(tse.Operators.Find(sales[i].OperatorID).Name);
                //list.Add(sales[i].BackFlag == 2 ? "退票单" : "出票单");
                //list.Add(sales[i].OriginGuid.ToString());
                dt.Rows.Add(list.ToArray());
            }
            this.dataGridView1.DataSource = dt;

            return this.dataGridView1;
        }


        //计算可以退的票数
        private int[] GetBackCount(Guid guid)
        {
            var unback = tse.Sales.Single(u => u.SaleGuid == guid);
            //var backed = tse.Sales.Where(u => u.OriginGuid == guid);
            var backed = tse.Backs.Where(u => u.BackNumber == guid.ToString());
            int[] canback = new int[5];
            canback[0] = unback.TicketOne;
            canback[1] = unback.TicketPark;
            canback[2] = unback.TicketParkSpecial;
            canback[3] = unback.TicketTwo;
            canback[4] = unback.TicketThree;
            foreach (var b in backed)
            {
                canback[0] -= b.BackOne;
                canback[1] -= b.BackPark;
                canback[2] -= b.BackParkSpecial;
                canback[3] -= b.BackTwo;
                canback[4] -= b.BackThree;
            }
            return canback;
        }
        private void btn_Search_Click(object sender, EventArgs e)
        {
            //InitData();
            BindData();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            txt_BackOne.Enabled = true; txt_BackPark.Enabled = true;
            txt_BackParkSpecial.Enabled = true; txt_BackTwo.Enabled = true;
            txt_BackThree.Enabled = true; btn_Update.Enabled = true;
            txt_One.Text = this.dataGridView1[0, row].Value.ToString();
            txt_Park.Text = this.dataGridView1[1, row].Value.ToString();
            txt_ParkSpecial.Text = this.dataGridView1[2, row].Value.ToString();
            txt_Two.Text = this.dataGridView1[3, row].Value.ToString();
            txt_Three.Text = this.dataGridView1[4, row].Value.ToString();
            lb_SaleGuid.Text = this.dataGridView1[13, row].Value.ToString();
            _saleGuid = Guid.Parse(this.dataGridView1[13, row].Value.ToString());
            int[] canBacks = GetBackCount(_saleGuid);
            txt_CanBackOne.Text = canBacks[0].ToString();
            txt_CanBackPark.Text = canBacks[1].ToString();
            txt_CanBackParkSpecial.Text = canBacks[2].ToString();
            txt_CanBackTwo.Text = canBacks[3].ToString();
            txt_CanBackThree.Text = canBacks[4].ToString();
            if (!string.IsNullOrEmpty(dataGridView1[11, row].Value.ToString()))
            {
                DateTime dtime = DateTime.Parse(dataGridView1[11, row].Value.ToString());
                int saleid = Convert.ToInt32(dataGridView1[12, row].Value);
                if (dtime.Date == DateTime.Now.Date && tse.Backs.Where(u => u.SaleID == saleid).Count() == 0)
                {                    
                    DialogResult d = MessageBox.Show("当天的票只能全退，是否继续", "确认退票", MessageBoxButtons.YesNo);
                    if (d == DialogResult.Yes)
                    {                        
                        Guid guid = Guid.Parse(this.dataGridView1[13, row].Value.ToString());
                        Sales oldSale = tse.Sales.Single(u => u.SaleGuid == guid);
                        oldSale.BackFlag = 1;//标识全退订单
                        txt_BackOne.Text = oldSale.TicketOne.ToString(); txt_BackPark.Text = oldSale.TicketPark.ToString();
                        txt_BackParkSpecial.Text = oldSale.TicketParkSpecial.ToString(); txt_BackTwo.Text = oldSale.TicketTwo.ToString();
                        txt_BackThree.Text = oldSale.TicketThree.ToString();
                        Backs back = new Backs();
                        back.BackOne = oldSale.TicketOne;
                        back.BackTwo = oldSale.TicketTwo;
                        back.BackThree = oldSale.TicketThree;
                        back.BackPark = oldSale.TicketPark;
                        back.BackParkSpecial = oldSale.TicketParkSpecial;
                        //back.BackMoney = oldSale.PurchaseMoney + oldSale.SignedMoney;//退款总金额
                        back.BackMoney = oldSale.PurchaseMoney;//退款总金额签单金额不退，直接退单子就行
                        back.BackNumber = oldSale.SaleGuid.ToString();
                        DateTime todaystart = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd ") + " 00:00:00");
                        int cnt = tse.Checkouts.Where(u => u.Checkout_at > todaystart).Count();
                        if (cnt > 0)
                        {
                            back.Created_at = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("yyyy-MM-dd ") + " 00:00:01");
                            back.Updated_at = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("yyyy-MM-dd ") + " 00:00:01");
                        }
                        else
                        {
                            back.Created_at = DateTime.Now;
                            back.Updated_at = DateTime.Now;
                        }
                        back.SaleID = oldSale.SaleID;
                        back.OperatorID = GetOperator.OperatorID;
                        tse.Backs.Add(back);
                        tse.SaveChanges();
                        MessageBox.Show("票号为:" + oldSale.SaleGuid + "(" + oldSale.SaleID + ")" + "的订票，已退票成功!");                        
                        BindData();
                        txt_SaleInfo.Focus();
                        SendKeys.Send("{END}+{HOME}");
                    }
                    txt_BackOne.Enabled = false; txt_BackPark.Enabled = false;
                    txt_BackParkSpecial.Enabled = false; txt_BackTwo.Enabled = false;
                    txt_BackThree.Enabled = false; btn_Update.Enabled = false;
                }
                
            }
        }

        private void btn_Update_Click(object sender, EventArgs e)
        {
            int back_one = !string.IsNullOrEmpty(txt_BackOne.Text) ? int.Parse(txt_BackOne.Text) : 0;
            int back_two = !string.IsNullOrEmpty(txt_BackTwo.Text) ? int.Parse(txt_BackTwo.Text) : 0;
            int back_three = !string.IsNullOrEmpty(txt_BackThree.Text) ? int.Parse(txt_BackThree.Text) : 0;
            int back_park = !string.IsNullOrEmpty(txt_BackPark.Text) ? int.Parse(txt_BackPark.Text) : 0;
            int back_parkspecial = !string.IsNullOrEmpty(txt_BackParkSpecial.Text) ? int.Parse(txt_BackParkSpecial.Text) : 0;
            var allTicketPrice = (from n in tse.TicketTypes select new { n.Price, n.SpecialPrice, n.Name }).ToList();
            decimal price_one = allTicketPrice.Find(u => u.Name == "一道船").Price;            
            decimal price_two = allTicketPrice.Find(u => u.Name == "二道船").Price;
            decimal price_three = allTicketPrice.Find(u => u.Name == "三道船").Price;
            decimal price_park = allTicketPrice.Find(u => u.Name == "公园票").Price;
            decimal price_parkspecial = allTicketPrice.Find(u => u.Name == "公园票").SpecialPrice;
            if (CheckBack(back_one, back_two, back_three, back_park, back_parkspecial))
            {
                Sales oldSale = tse.Sales.Single(u => u.SaleGuid == _saleGuid);
                Backs back = new Backs();
                back.OperatorID = GetOperator.OperatorID;
                back.SaleID = oldSale.SaleID;
                back.BackNumber = _saleGuid.ToString();
                DateTime todaystart = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd ") + " 00:00:00");
                int cnt = tse.Checkouts.Where(u => u.Checkout_at > todaystart).Count();
                if (cnt > 0)
                {
                    back.Created_at = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("yyyy-MM-dd ") + " 00:00:01");
                    back.Updated_at = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("yyyy-MM-dd ") + " 00:00:01");
                }
                else
                {
                    back.Created_at = DateTime.Now;
                    back.Updated_at = DateTime.Now;
                }
                back.BackOne = back_one;
                back.BackPark = back_park;
                back.BackParkSpecial = back_parkspecial;
                back.BackTwo = back_two;
                back.BackThree = back_three;                
                //back.BackMoney = (price_one * back_one) + (price_park * back_park) + (price_parkspecial * back_parkspecial) + (price_two * back_two) + (price_three * back_three);
                back.BackMoney = decimal.Parse(txt_backMoney_Total.Text);
                try
                {
                    DialogResult dresult = MessageBox.Show("请确认以下退票信息\r\n退票时间：" + DateTime.Now + "\r\n首套船退：" + back.BackOne.ToString() + "张\r\n公园成人票退：" + back.BackPark.ToString() + "张\r\n公园学生票退：" + back.BackParkSpecial.ToString() + "张\r\n二道船退：" + back.BackTwo.ToString() + "张\r\n三道船退：" + back.BackThree.ToString() + "张", "确认退票", MessageBoxButtons.OKCancel);
                    if (dresult == DialogResult.OK)
                    {
                        tse.Backs.Add(back);
                        tse.SaveChanges();
                        MessageBox.Show("流水号为:" + back.SaleID.ToString() + ",票号为:" + back.BackNumber + ",退单成功");                 
                        BindData();
                        txt_SaleInfo.Focus();
                        SendKeys.Send("{END}+{HOME}");
                        //txt_SaleInfo.SelectAll();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
            else
            {
                return;
            }
        }

        //添加退单记录
        private void AddBackLog(int saleID)
        {
            Backs back = new Backs();
            back.OperatorID = GetOperator.OperatorID;
            back.SaleID = saleID;
            back.BackNumber = Guid.NewGuid().ToString();
            back.Created_at = DateTime.Now;
            back.Updated_at = DateTime.Now;
            tse.Backs.Add(back);
            tse.SaveChanges();
        }

        private bool CheckBack(int back_one, int back_two, int back_three, int back_park, int back_parkspecial)
        {
            if (back_one > int.Parse(txt_CanBackOne.Text) || back_two > int.Parse(txt_CanBackTwo.Text) || back_three > int.Parse(txt_CanBackThree.Text) || back_park > int.Parse(txt_CanBackPark.Text) || back_parkspecial > int.Parse(txt_CanBackParkSpecial.Text))
            {
                MessageBox.Show("退票数不能超过可退票数");
                return false;
            }
            else if (back_one == 0 && back_two == 0 && back_three == 0 && back_park == 0 && back_parkspecial == 0)
            {
                MessageBox.Show("没有输入任何退票数据");
                return false;
            }
            else
                return true;
        }

        //限制只可以输入数字
        private void txt_Number_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键  
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字  
                {
                    e.Handled = true;
                }
            }
        }

        //限制只可以输入GUID
        private void txt_GUID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z')
                || (e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8) || (e.KeyChar == 45))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        //查询回车事件
        private void PressEnter_Search_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btn_Search_Click(sender, e);
            }
        }
        //修改回车事件
        private void PressEnter_Update_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btn_Update_Click(sender, e);
            }
        }
        //回车事件（下一个）
        private void PressEnter_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                System.Windows.Forms.SendKeys.Send("{TAB}");
            }
        }
        #region
        private void txt_BackOne_TextChanged(object sender, EventArgs e)
        {
            if ((!string.IsNullOrEmpty(txt_BackOne.Text) ? int.Parse(txt_BackOne.Text) : 0) > int.Parse(txt_CanBackOne.Text))
            {
                MessageBox.Show("退票数不能超过已定票数");
                txt_BackOne.Text = "";
            }
            else
            {
                string _count = string.IsNullOrEmpty(txt_BackOne.Text) ? "0" : txt_BackOne.Text;
                txt_backMoney_One.Text = (tse.TicketTypes.Find(1).Price * decimal.Parse(_count)).ToString();
                Guid guid = Guid.Parse(lb_SaleGuid.Text);
                var sale = tse.Sales.Where(u => u.SaleGuid == guid).First();
                if (sale.SignedPeople > 0)
                {
                    txt_backMoney_One.Text = "0";
                }
                getBackTotal();
            }
        }

        private void txt_BackTwo_TextChanged(object sender, EventArgs e)
        {
            if ((!string.IsNullOrEmpty(txt_BackTwo.Text) ? int.Parse(txt_BackTwo.Text) : 0) > int.Parse(txt_CanBackTwo.Text))
            {
                MessageBox.Show("退票数不能超过可退票数");
                txt_BackTwo.Text = "";
            }
            else
            {
                string _count = string.IsNullOrEmpty(txt_BackTwo.Text) ? "0" : txt_BackTwo.Text;
                txt_backMoney_Two.Text = (tse.TicketTypes.Find(2).Price * decimal.Parse(_count)).ToString();
                getBackTotal();
            }
        }

        private void txt_BackThree_TextChanged(object sender, EventArgs e)
        {
            if ((!string.IsNullOrEmpty(txt_BackThree.Text) ? int.Parse(txt_BackThree.Text) : 0) > int.Parse(txt_CanBackThree.Text))
            {
                MessageBox.Show("退票数不能超过可退票数");
                txt_BackThree.Text = "";
            }
            else
            {
                string _count = string.IsNullOrEmpty(txt_BackThree.Text) ? "0" : txt_BackThree.Text;
                txt_backMoney_Three.Text = (tse.TicketTypes.Find(3).Price * decimal.Parse(_count)).ToString();
                getBackTotal();
            }
        }

        private void txt_BackPark_TextChanged(object sender, EventArgs e)
        {
            if ((!string.IsNullOrEmpty(txt_BackPark.Text) ? int.Parse(txt_BackPark.Text) : 0) > int.Parse(txt_CanBackPark.Text))
            {
                MessageBox.Show("退票数不能超过可退票数");
                txt_BackPark.Text = "";
            }
            else
            {
                string _count = string.IsNullOrEmpty(txt_BackPark.Text) ? "0" : txt_BackPark.Text;
                txt_backMoney_Park.Text = (tse.TicketTypes.Find(4).Price * decimal.Parse(_count)).ToString();
                getBackTotal();
            }
        }

        private void txt_BackParkSpecial_TextChanged(object sender, EventArgs e)
        {
            if ((!string.IsNullOrEmpty(txt_BackParkSpecial.Text) ? int.Parse(txt_BackParkSpecial.Text) : 0) > int.Parse(txt_CanBackParkSpecial.Text))
            {
                MessageBox.Show("退票数不能超过可退票数");
                txt_BackParkSpecial.Text = "";
            }
            else
            {
                string _count = string.IsNullOrEmpty(txt_BackParkSpecial.Text) ? "0" : txt_BackParkSpecial.Text;
                txt_backMoney_ParkStudent.Text = (tse.TicketTypes.Find(4).SpecialPrice * decimal.Parse(_count)).ToString();
                getBackTotal();
            }

        }

        private void getBackTotal()
        {
            List<string> list = new List<string>();
            list.Add(string.IsNullOrEmpty(txt_backMoney_One.Text) ? "0" : txt_backMoney_One.Text);
            list.Add(string.IsNullOrEmpty(txt_backMoney_Park.Text) ? "0" : txt_backMoney_Park.Text);
            list.Add(string.IsNullOrEmpty(txt_backMoney_ParkStudent.Text) ? "0" : txt_backMoney_ParkStudent.Text);
            list.Add(string.IsNullOrEmpty(txt_backMoney_Two.Text) ? "0" : txt_backMoney_Two.Text);
            list.Add(string.IsNullOrEmpty(txt_backMoney_Three.Text) ? "0" : txt_backMoney_Three.Text);
            decimal Total = 0;
            foreach (string l in list)
            {
                if (!string.IsNullOrEmpty(l))
                    Total += decimal.Parse(l);
            }
            txt_backMoney_Total.Text = Total.ToString();
        }

        #endregion

        private void txt_SaleInfo_TextChanged(object sender, EventArgs e)
        {
            InitData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string msg = "";
            DateTime start = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd ") + " 00:00:00");
            DateTime end = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd ") + " 23:59:59");
            int cnt = tse.Backs.Where(u => u.Created_at > start && u.Created_at < end).Count();
            if (cnt > 0)
            {                
                var lastback = tse.Backs.Where(u => u.Created_at > start && u.Created_at < end).OrderByDescending(u=>u.Created_at).First();
                msg = "上次退票流水号为：" + lastback.SaleID + ",\r\n票号为：" + lastback.BackNumber + ",\r\n首道船退票：" + lastback.BackOne + "张,\r\n公园成人票退票：" +
                    lastback.BackPark + "张,\r\n公园学生票退票：" + lastback.BackParkSpecial + "张,\r\n二道船退票：" + lastback.BackTwo + "张,\r\n三道船退票：" +
                    lastback.BackThree + "张,\r\n退票总金额：" + lastback.BackMoney + "。";
            }
            else
            {                
                msg = "今天还没有退票数据产生！";
            }
            MessageBox.Show(msg);
        }

        private void checkBox_DateSpan_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_DateSpan.Checked)
            {
                dTP_Date.Enabled = true;
            }
            else
            {
                dTP_Date.Enabled = false;
            }
        }

        private void txt_BackTwo_Enter(object sender, EventArgs e)
        {
            //int[] canBacks = GetBackCount(_saleGuid);
            this.txt_BackTwo.Text = txt_CanBackTwo.Text;
        }

        private void txt_BackThree_Enter(object sender, EventArgs e)
        {
            this.txt_BackThree.Text = txt_CanBackThree.Text;
        }

        private void Form_Back_Load(object sender, EventArgs e)
        {
            var _opes = tse.Operators.Select(u => new { u.OperatorID, u.Name }).ToList();
            cBox_Operator.DataSource = _opes;
            cBox_Operator.ValueMember = "OperatorID";
            cBox_Operator.DisplayMember = "Name";
            var _OpeSource = new AutoCompleteStringCollection();
            foreach (var _ope in _opes)
            {
                _OpeSource.Add(_ope.Name);
            }
            cBox_Operator.AutoCompleteCustomSource = _OpeSource;
            cBox_Operator.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cBox_Operator.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cBox_Operator.Text = "";
        }
    }
}
